package com.oa.service.impl;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.oa.bean.IntegernalMail;
import com.oa.bean.Section;
import com.oa.bean.User;
import com.oa.dao.IntegernalMailDao;
import com.oa.service.IntegernalMailService;

@Service
public class IntegernalMailServiceImpl implements IntegernalMailService{

	@Resource
	IntegernalMailDao integernalMailDao;
	
	@Override
	public List<User> getListUser(Integer deptno) {
		// TODO Auto-generated method stub
		List<User> list = integernalMailDao.getUserList(deptno);
		return list;
	}

	@Override
	public String sendMail(IntegernalMail mail) {
		// TODO Auto-generated method stub
		//想邮件表中插入数据
		System.out.println(mail.getEid());
		Integer result = integernalMailDao.insertMail(mail);
		System.out.println(mail.getEid());
		String replyName = mail.getEdirection_id();
		String[]array = replyName.split(",");
		System.out.println(Arrays.toString(array));
		
		Map<String,Object> map = new HashMap<>();
		map.put("ridList", array);
		map.put("email_id", mail.getEid());
		Integer resul = integernalMailDao.insertReceive_Mail(map);
		System.out.println(resul);
		return null;
	}

	@Override
	public Map<String, Object> getMyEmailList(Integer currentPage, Integer uid) {
		// TODO Auto-generated method stub
		Page page=PageHelper.startPage(currentPage, 2,"eid desc");
		List<IntegernalMail> list = integernalMailDao.selectMyEmail(uid);
		Map<String,Object> map = new HashMap<>();
		map.put("emailList", list);
		map.put("mixNum", page.getPages());
		map.put("limit", 2);
		return map;
	}

	@Override
	public Map<String, Object> getReceiveEmail(Integer currentPage, String userName) {
		// TODO Auto-generated method stub
		Page page=PageHelper.startPage(currentPage, 2,"eid desc");
		List<IntegernalMail> list = integernalMailDao.selectReceiveEmail(userName);
		Map<String,Object> map = new HashMap<>();
		map.put("receiveList", list);
		map.put("mixNum", page.getPages());
		map.put("limit", 2);
		return map;
	}

	@Override
	public IntegernalMail getEmailOne(String rstate, Integer eid,String userName) {
		// TODO Auto-generated method stub
		IntegernalMail mail = integernalMailDao.selectEmailByid(eid);
		if("未读".equals(rstate)){
			Map<String,Object> map = new HashMap<>();
			map.put("userName", userName);
			map.put("eid", eid);
			integernalMailDao.updateReceiveRstate(map);
		}
		return mail;
	}

	@Override
	public String delectMySendMail(String text) {
		// TODO Auto-generated method stub
		String[]arr=text.split(",");
		int[]array = new int[2];
		for(int i=0;i<arr.length;i++){
			array[i]=Integer.parseInt(arr[i]);
		}
		System.out.println(Arrays.toString(array));
		Integer result = integernalMailDao.deleteSendMail(array);
		System.out.println("批量删除发送的邮件");
		if(result!=0){
			return "true";
		}
		return "false";
	}

	@Override
	public String delectMyReceive(String text, String userName) {
		// TODO Auto-generated method stub
		String[]arr=text.split(",");
		int[]array = new int[2];
		for(int i=0;i<arr.length;i++){
			array[i]=Integer.parseInt(arr[i]);
		}
		System.out.println(Arrays.toString(array));
		Map<String,Object> map = new HashMap<>();
		map.put("receive_id", userName);
		map.put("array", array);
		Integer result = integernalMailDao.deleteReceiveMail(map);
		System.out.println(result);
		if(result!=0){
			return "true";
		}
		return "false";
	}

	@Override
	public List<Section> getDeptnoandName() {
		// TODO Auto-generated method stub
		List<Section> list = integernalMailDao.selectDeptNameId();
		return list;
	}

	
	
	
}
